package com.dehui.traders.ui.register;

import android.app.ProgressDialog;
import android.content.Context;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.R;
import com.dehui.traders.utils.DialogHelp;
import com.dehui.traders.utils.MD5;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;
import retrofit2.Call;
import retrofit2.Response;

public class RegisterViewModel extends BaseViewModel{

    public ObservableBoolean agreementSelect = new ObservableBoolean(true);

    public ObservableField<String> phone = new ObservableField<>("");

    public ObservableField<String> code = new ObservableField<>("");

    public ObservableField<String> pwd = new ObservableField<>("");

    public ObservableField<String> pwd1 = new ObservableField<>("");

    public BindingCommand agreemntOnClick = new BindingCommand(()->{
        agreementSelect.set(!agreementSelect.get());
    });

    public BindingCommand getCodeOnClick = new BindingCommand(()->{
        getCode();
    });

    public BindingCommand registerOnClick = new BindingCommand(()->{
        register();
    });

    private ProgressDialog loadingDialog;
    private RegisterActivity activity;

    private void register() {
        if (TextUtils.isEmpty(phone.get())){
            ToastUtils.showShort(R.string.please_input_phone);
        }else if (TextUtils.isEmpty(code.get())){
            ToastUtils.showShort(R.string.please_input_code);
        }else if (TextUtils.isEmpty(pwd.get()) || TextUtils.isEmpty(pwd1.get())){
            ToastUtils.showShort(R.string.please_input_password);
        }else if(pwd.get().length() < 6 || pwd.get().length() > 16){
            ToastUtils.showShort(R.string.please_enter_the_correct_bit_password);
        }else if(!TextUtils.equals(pwd.get(),pwd1.get())){
            ToastUtils.showShort(R.string.password_dont_agree);
        }else if (!agreementSelect.get()){
            ToastUtils.showShort(R.string.please_agree_user_agreement);
        }else{
            verifyCode();
        }
    }

    private void verifyCode() {
        SMSSDK.submitVerificationCode("86", phone.get(), code.get());
    }

    private void toRegister() {
        loadingDialog = DialogHelp.getLoadingDialog(context);
        loadingDialog.show();
        Call<BaseResponse<String>> call = NetImpl.register(phone.get(), MD5.md5(pwd.get()).toLowerCase(), code.get());
        call.enqueue(new CallListener<BaseResponse<String>>() {
            @Override
            public void _onResponse(Response<BaseResponse<String>> response) {
                loadingDialog.dismiss();
                BaseResponse<String> baseResponse = response.body();
                int status = baseResponse.status;
                if (status == 200){
                    ToastUtils.showShort(R.string.register_success);
                    activity.finish();
                }
            }

            @Override
            public void _onFailure() {
                loadingDialog.dismiss();
                ToastUtils.showShort(R.string.net_expection);
            }
        });
    }


    private void getCode() {
        if (TextUtils.isEmpty(phone.get())){
            ToastUtils.showShort(R.string.please_input_phone);
            return;
        }
        ((RegisterActivity)context).startButton();
        SMSSDK.getVerificationCode("86", phone.get());
    }


    public RegisterViewModel(Context context) {
        super(context);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SMSSDK.registerEventHandler(eh);
    }

    EventHandler eh=new EventHandler(){

        @Override
        public void afterEvent(int event, int result, Object data) {
            KLog.i("Hqq","event=>" + event + "result=>" + result + "data=>" + data);
            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                    mHandler.sendEmptyMessage(1);
                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                    //获取验证码成功
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                    //返回支持发送验证码的国家列表
                }
            }else{
                ((Throwable)data).printStackTrace();
                mHandler.sendEmptyMessage(0);
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eh);
    }

    public void initActivity(RegisterActivity activity) {
        this.activity = activity;
    }


    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            if (what == 0){
                ToastUtils.showShort("验证码错误");
            }else if (what == 1){
                toRegister();
            }
        }
    };


}
